package com.example.result;

import com.example.entity.Unit;

import java.util.ArrayList;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: tobed
 * @date: 2022/11/20
 * @time: 11:18
 */
public class Test {

    public static void main2(String[] args) {
        ArrayList<Unit> units = new ArrayList<>();
        units.add(new Unit().setUnit("hahaha").setUnitNumber("123"));
        units.add(new Unit().setUnit("hahaha1").setUnitNumber("456"));
        units.add(new Unit().setUnit("hahaha2").setUnitNumber("789"));
        units.add(new Unit().setUnit("hahaha3").setUnitNumber("111"));
        units.add(new Unit().setUnit("hahaha4").setUnitNumber("222"));
        Map<String, Unit> collect = units.stream()
                /*
                 *new Function<Unit(list集合中的类型), String(key)>()
                 *
                 */
                .collect(Collectors.toMap(unit -> unit.getUnit(), unit -> unit));
                collect.forEach((BiConsumer) (o, o2) -> System.out.println(o + "," + o2));
    }

    public static void main(String[] args) {
        Stream<Integer> integerStream = Stream.of(10, 20, 50, 100,10);
        Optional<Integer> reduce = integerStream.reduce(new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer a1, Integer a2) {
                return a1+a2;
            }
        });
        System.out.println(reduce.get());
    }


}
